<?php

class Emms_Form_Admin_Community extends HTech_Form
{
    protected $_primary  = 'CommunityId';
    
    public function __construct() 
    {
        parent::__construct();        
        
        $this->setName('Community');
        $this->setAction('/admin/community/add');
        
        $this->setAttrib('title', $this->translate("Community form"));
        $this->setDescription($this->translate("Edit community info or add a new one"));
        
        $communityId = new Zend_Form_Element_Hidden('CommunityId');
        
        $community = new Zend_Form_Element_Text('Community');
        $community->setLabel("Community")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 64,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ))
            ->setDescription(null);
                
        $locationId = new Zend_Form_Element_Select('LocationId');
        $locationId->setLabel("Location")                
            ->setAttrib('size','5')
            ->setRequired(true)
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkLocationIdCallback'),
                'messages' => $this->translate("Location can't be changed")))
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions($this->_db->fetchPairs('select LocationId, Name from locations'));          
        
        $type = new Zend_Form_Element_Select('Type');
        $type->setLabel("Type")                
            ->setAttrib('size','3')
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addMultiOptions(array('Urban' => $this->translate("Urban"), 'Suburban' => $this->translate("Suburban"), 'Rural' => $this->translate("Rural"))); 
        
        $latitude = new Zend_Form_Element_Text('Latitude');
        $latitude->setLabel("Latitude")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^(-)?\d{1,3}+(\.\d{1,7})?$)',
                'messages' => $this->translate("Only valid latitude coordinates allowed")))
            ->addValidator('Between', true, array(
                'min' => $this->_conf->geo->latitude->min,
                'max' => $this->_conf->geo->latitude->max,
                'messages' => $this->translate("Out of range value")));
                                
        $longitude = new Zend_Form_Element_Text('Longitude');
        $longitude->setLabel("Longitude")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^(-)?\d{1,3}+(\.\d{1,7})?$)',
                'messages' => $this->translate("Only valid longitude coordinates allowed")))
            ->addValidator('Between', true, array(
                'min' => $this->_conf->geo->longitude->min,
                'max' => $this->_conf->geo->longitude->max,
                'messages' => $this->translate("Out of range value")));
                        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->addFilter('Alnum', array('allowwhitespace' => true))
            ->setAttrib('rows', 4); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($community,$locationId,$type,$latitude,$longitude,$comments,$submit,$communityId));
    }
    
    public function checkLocationIdCallback($locationId, $args) 
    {
        if($locationId == $this->_db->fetchOne('select LocationId from communities where CommunityId = ?', $args['CommunityId']))
            return true;
        if($this->_db->fetchOne('select count(ClientId) from clients where CommunityId = ?', $args['CommunityId']))
            return false;
        return true;
    }
}